home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 029a / fcopyc10.zip / FARREAD.ASM < prev    next >
Assembly Source File  |  1992-01-05  |  10KB  |  181 lines

  1. ;-----------------------------------------------------------------------;
  2. ; FARREAD.ASM                                                           ;
  3. ;                                                                       ;
  4. ; This module contains functions that duplicate the library functions   ;
  5. ; _read() and _write(), except that these expect far pointers to the    ;
  6. ; file buffer.  Thus, when used with farmalloc(), the calling program   ;
  7. ; can set up large file buffers using the far heap, regardless of the   ;
  8. ; memory model being used.                                              ;
  9. ;                                                                       ;
  10. ; The len variable, indicating the buffer size, can be any int sized    ;
  11. ; value up to (but not including) 65,535.  65,535 is also used to       ;
  12. ; represent -1, which is used as the return value in case of error.     ;
  13. ; It is recommended that the buffer be a multiple of the sector size    ;
  14. ; (512 bytes in the current version of DOS), since reads and writes     ;
  15. ; are more efficient if they involve whole sectors.  A good value to    ;
  16. ; use is 65024 (decimal), since this is 1 sector (512 bytes) short      ;
  17. ; of a complete segment (64k).                                          ;
  18. ;-----------------------------------------------------------------------;
  19. ; Usage:                                                                ;
  20. ;                                                                       ;
  21. ; #include "fcopy.h"                                                    ;
  22. ;                                                                       ;
  23. ; Function prototypes:                                                  ;
  24. ;                                                                       ;
  25. ; int _farread (int handle, void far *buf, unsigned len)                ;
  26. ;                                                                       ;
  27. ;          Returns the number of bytes read;                            ;
  28. ;          on end-of-file, returns 0;                                   ;
  29. ;          on error returns -1 and sets both errno and _doserrno.       ;
  30. ;                                                                       ;
  31. ; int _farwrite (int handle, void far *buf, unsigned len)               ;
  32. ;                                                                       ;
  33. ;          Returns the number of bytes written;                         ;
  34. ;          on error returns -1 and sets both errno and _doserrno.       ;
  35. ;-----------------------------------------------------------------------;
  36. ; Revision history:                                                     ;
  37. ;                                                                       ;
  38. ;       1.0      5 JAN 92       Original.                               ;
  39. ;                                                                       ;
  40. ;                               Uses conditional assembly for all       ;
  41. ;                               memory models by defining _model.       ;
  42. ;                               Defaults to the small model if          ;
  43. ;                               _model is not defined.                  ;
  44. ;                                                                       ;
  45. ;                               Uses TASM 2.0 extensions, so this       ;
  46. ;                               source file will need modifications     ;
  47. ;                               to assemble under MASM 5.1 or 6.0.      ;
  48. ;-----------------------------------------------------------------------;
  49. ;   Copyright (c) 1992 Ray Waters                                       ;
  50. ;   All Rights Reserved                                                 ;
  51. ;-----------------------------------------------------------------------;
  52.  
  53. IFNDEF  _model                          ; if a _model directive was
  54.         .MODEL  SMALL, C                ; not defined, default to the
  55. ELSE                                    ; SMALL model
  56. %        .MODEL   _model, C             ; else, use the model indicated
  57. ENDIF
  58.  
  59.         EXTRN   C errno:WORD            ; global error variable
  60.         EXTRN   C _doserrno:WORD        ; global DOS error variable
  61.  
  62.         .CODE                           ; open code segment
  63.  
  64.         PUBLIC  _farread                ; make visible to Linker
  65. ;-----------------------------------------------------------------------;
  66. ; int _farread (int handle, void far *buf, unsigned len)                ;
  67. ;                                                                       ;
  68. ; Reads a file into a far buffer.                                       ;
  69. ;                                                                       ;
  70. ; Returns:  If successful, returns number of bytes read.  On            ;
  71. ;           end-of-file, returns 0.                                     ;
  72. ;                                                                       ;
  73. ;           If read failed, returns -1 and sets errno and _doserrno     ;
  74. ;           to one of the following:                                    ;
  75. ;                                                                       ;
  76. ;               EACCES  Permission denied                               ;
  77. ;               EBADF   Bad file number                                 ;
  78. ;-----------------------------------------------------------------------;
  79. _farread        PROC    C
  80.  
  81.         ARG     handle:WORD, buf:FAR PTR BYTE, len:WORD
  82.  
  83.         push    ds                      ; save data segment
  84.         mov     ah,3Fh                  ; DOS read file function
  85.         mov     bx,[handle]             ; file handle
  86.         mov     cx,[len]                ; number bytes to read
  87.         lds     dx,[buf]                ; far pointer to buffer
  88.         int     21h
  89.         pop     ds                      ; restore data segment
  90.  
  91.         jc      @@read_error            ; jump on error, else...
  92.         ret                             ; return to caller
  93.  
  94. @@read_error:
  95.  
  96. IF @DataSize EQ 2                       ; if HUGE memory model,
  97.         push    ds                      ; save DS
  98.         mov     bx,SEG errno            ; get segment address
  99.         mov     ds,bx                   ;  of errno into DS
  100. ENDIF
  101.         mov     [errno],ax              ; set errno
  102.         mov     [_doserrno],ax          ; set _doserrno
  103.         mov     ax,-1                   ; return int value of -1
  104.  
  105. IF @DataSize EQ 2                       ; if HUGE memory model,
  106.         pop     ds                      ; restore DS
  107. ENDIF
  108.         ret                             ; return to caller
  109.  
  110. _farread        ENDP
  111.  
  112.         PUBLIC  _farwrite               ; make visible to Linker
  113. ;-----------------------------------------------------------------------;
  114. ; int _farwrite (int handle, void far *buf, unsigned len)               ;
  115. ;                                                                       ;
  116. ; Writes to a file from a far buffer.                                   ;
  117. ;                                                                       ;
  118. ; Returns:  If successful, returns number of bytes written.             ;
  119. ;                                                                       ;
  120. ;           If write failed, returns -1 and sets errno amd _doserrno    ;
  121. ;           to one of the following:                                    ;
  122. ;                                                                       ;
  123. ;               EACCES  Permission denied                               ;
  124. ;               EBADF   Bad file number                                 ;
  125. ;                   -1  Target disk full                                ;
  126. ;-----------------------------------------------------------------------;
  127. _farwrite       PROC    C
  128.  
  129.         ARG     handle:WORD, buf:FAR PTR BYTE, len:WORD
  130.  
  131.         push    ds                      ; save data segment
  132.         mov     ah,40h                  ; DOS write file function
  133.         mov     bx,[handle]             ; file handle
  134.         mov     cx,[len]                ; number bytes to write
  135.         lds     dx,[buf]                ; far pointer to buffer
  136.         int     21h
  137.         pop     ds                      ; restore data segment
  138.  
  139.         jc      @@write_error           ; jump on error
  140.  
  141.         cmp     ax,cx                   ; were all bytes written?
  142.         jne     @@partial_write_error   ; if not, partial write error
  143.  
  144.         ret                             ; return to caller
  145.  
  146. @@partial_write_error:
  147.  
  148. IF @DataSize EQ 2                       ; if HUGE memory model,
  149.         push    ds                      ; save DS
  150.         mov     bx,SEG errno            ; get segment address
  151.         mov     ds,bx                   ;  of errno into DS
  152. ENDIF
  153.         mov     ax,-1                   ; return int value of -1
  154.         mov     [errno],ax              ; set errno to -1
  155.         mov     [_doserrno],ax          ; set _doserrno to -1
  156.  
  157. IF @DataSize EQ 2                       ; if HUGE memory model,
  158.         pop     ds                      ; restore DS
  159. ENDIF
  160.         ret                             ; return to caller
  161.  
  162. @@write_error:                          ; if write error,
  163.  
  164. IF @DataSize EQ 2                       ; if HUGE memory model,
  165.         push    ds                      ; save DS
  166.         mov     bx,SEG errno            ; get segment address
  167.         mov     ds,bx                   ;  of errno into DS
  168. ENDIF
  169.         mov     [errno],ax              ; set errno
  170.         mov     [_doserrno],ax          ; set _doserrno
  171.         mov     ax,-1                   ; return int value of -1
  172.  
  173. IF @DataSize EQ 2                       ; if HUGE memory model,
  174.         pop     ds                      ; restore DS
  175. ENDIF
  176.         ret                             ; return to caller
  177.  
  178. _farwrite       ENDP
  179.  
  180.         END
  181.